import React, { Component } from "react";
import { Modal, Toast } from "antd-mobile";
import copy from "copy-to-clipboard";
import yhTools from "@/tools/yh";

import "./index.scss";

class inviteModal extends Component {
	constructor(props) {
		super(props);
		this.state = {
			visible: false,
			inviteInfo: {} //分享数据
		};
	}
	// componentDidMount() {
	// 	this.props.onRef(this);
	// }
	//打开模态框
	open() {
		this.setState({
			visible: true
		});
	}
	//关闭模态框
	close() {
		this.setState({
			visible: false
		});
	}

	//设置邀请数据
	setInviteInfo(inviteInfo) {
		this.setState(
			{
				inviteInfo
			},
			() => {
				this.open();
			}
		);
	}
	//微信分享
	weiXinShare(channel) {
		if (this.getSdkVersion() < 102) {
			Toast.info("您当前的版本过低，暂不支持分享，请更新至最新版本");
			return;
		}
		let inviteInfo = this.state.inviteInfo;
		yhTools.wxShareWebPage({
			webpageUrl: `${inviteInfo[channel].shareUrl}`,
			title: inviteInfo[channel].parentTitle,
			description: inviteInfo[channel].subTitle,
			type: "0", //0：url分享
			channel // 0:朋友圈，1:好友
		});
	}
	//判断版本号大于
	getSdkVersion() {
		let num = Number(this.props.versionCode.replace(/\./g, ""));
		return num;
	}
	//姓脱敏
	nameStar(name) {
		return name.substring(0, 1);
	}
	//复制链接
	copyLink(channel) {
		const inviteInfo = this.state.inviteInfo;

		copy(inviteInfo[channel].linkTitle);
		Toast.info("复制成功");
	}
	render() {
		return (
			<Modal
				className="invite_modal"
				popup
				visible={this.state.visible}
				onClose={() => {
					this.close();
				}}
				animationType="slide-up"
			>
				<div className="invite_modal_content">
					<div className="item_box">
						<div
							className="item"
							onClick={() => {
								this.weiXinShare("1");
							}}
						>
							<img src={require("@/images/invite/invite_weixin.png")} />
							<p>微信好友</p>
						</div>
						<div
							className="item"
							onClick={() => {
								this.weiXinShare("0");
							}}
						>
							<img
								src={require("@/images/invite/invite_pengyouquan.png")}
								alt=""
							/>
							<p>微信朋友圈</p>
						</div>
						<div
							className="item"
							onClick={() => {
								this.copyLink("2");
							}}
						>
							<img src={require("@/images/invite/invite_copy.png")} />
							<p>复制链接</p>
						</div>
					</div>
					<div
						className="cancel"
						onClick={() => {
							this.close();
						}}
					>
						取消
					</div>
				</div>
			</Modal>
		);
	}
}
export default inviteModal;
